package client;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * This is the frame for the logon screen and the main screen
 */
public class ClientFrame extends JFrame {

	private static final long serialVersionUID = 1L; 
	
	private static final int FRAME_HEIGHT = 435;
	private static final int FRAME_WIDTH = 590;
	private static final String FRAME_TITLE = "RPS";
	
	private LogonPanel logonPanel = null;
	private ClientPanel clientPanel = null;
	
	WindowListener windowListenerForGame = null;
	
	
    public ClientFrame () {   
    	// This enables the user to activate buttons with the focus on them via the Enter as well, and
    	// not only via Spacebar
    	try {
    		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    	}
    	catch(Exception e){
    		System.out.println(e.getLocalizedMessage());
    	}

		// This setDefaultCloseOperation will make the windows not close on a close event unless we specifically tell it to.
		// Otherwise there will be no way to cancel it from closing, if the user changes his mind.
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    	switchToLogon();
	} 
    
	public static void main (String [] args) {

		SwingUtilities.invokeLater(new Runnable() {
	        public void run() {
	    		ClientFrame clientFrame = new ClientFrame ();
	    		// Display connection panel
	    		clientFrame.setResizable(false);
	    		clientFrame.setVisible (true);
	    		clientFrame.validate();          
	        }
		});
	}
	
	public void connectOK (ConnectionThreadRPSClientToServer conn) {

		clientPanel = new ClientPanel (conn);
		
		// Remove logon panel and add client panel.
		getContentPane().remove (logonPanel);
		getContentPane().add    (clientPanel);
    	setSize(FRAME_WIDTH, FRAME_HEIGHT);
    	setResizable(true);
		clientPanel.revalidate();
		
		removeWindowListener(windowListenerForGame);

		windowListenerForGame = new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				clientPanel.closeClient ();
			}
		};
		
		addWindowListener(windowListenerForGame);
		
	    Dimension screenSize = getToolkit().getScreenSize();
		setLocation((int)screenSize.getWidth() / 2 - this.getWidth() / 2, (int)(screenSize.getHeight() - this.getHeight()) / 5);
	}
	
	public void switchToLogon()
	{
    	this.setTitle(FRAME_TITLE);
 
    	getContentPane().removeAll();
    	
		logonPanel = new LogonPanel (this);
		getContentPane().add (logonPanel);
		pack();
		
		removeWindowListener(windowListenerForGame);

		windowListenerForGame = new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				logonPanel.closeClient ();
			}
		};
		
		addWindowListener(windowListenerForGame);
		
	    Dimension screenSize = getToolkit().getScreenSize();
		setLocation((int)screenSize.getWidth() / 2 - this.getWidth() / 2, (int)(screenSize.getHeight() - this.getHeight()) / 5);
		
		setResizable(false);
		setVisible(true);
		validate();          
	}
	
	public ClientPanel getRpsClientPanel() {
		return clientPanel;
	}
}
